package com.zhangye.test.api;

import java.util.SortedMap;
import java.util.TreeMap;

import com.zhangye.test.model.ClientCustomSSL;
import com.zhangye.test.model.EnterprisesPayment;
import com.zhangye.test.util.WeixinpayUtil;

public class WeChat {
	
	
	
	 private static SortedMap<String,String> sortedMap = new TreeMap<>();
	 
	    public static void main(String[] args) {
	 
	        String sginCode = getSgin("openid",1);//获取用户openid 和 用户要提现的金额  拿到签名
	        EnterprisesPayment enterprisesPayment  = addEnterprisesPayment("openid",1,sginCode);//拿到签名后加密得到要发送到对象数据
	        String enterprisesPaymentXML = WeixinpayUtil.createDocumentForEnterprisesPayment(enterprisesPayment);  //封装xml 数据发送
	        try {
				String returnXmlData =  ClientCustomSSL.doRefund("url地址",enterprisesPaymentXML);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	 
	 
	    }
	 
	 
	    public static EnterprisesPayment addEnterprisesPayment(String openid, Integer amount,String sginCode){
	        EnterprisesPayment enterprisesPayment = new EnterprisesPayment();
	        enterprisesPayment.setMch_appid("wx10331e9b86cb6027");//商户号appid
	        enterprisesPayment.setMchid("1406370302");//商户号 
	        // enterprisesPayment.setDevice_info(null);//设备号 非必填 
	        enterprisesPayment.setNonce_str(sortedMap.get("nonce_str"));//随机字符串 
	        enterprisesPayment.setSign(sginCode);//签名 
	        enterprisesPayment.setPartner_trade_no(sortedMap.get("partner_trade_no"));//商户订单号 
	        enterprisesPayment.setOpenid(openid);  
	        enterprisesPayment.setCheck_name("NO_CHECK");
	        enterprisesPayment.setRe_user_name(null); //根据checkName字段判断是否需要 
	        enterprisesPayment.setAmount(amount);//金额 
	        enterprisesPayment.setDesc("desc");//描述 
	        enterprisesPayment.setSpbill_create_ip("ip");//ip地址 
	        return enterprisesPayment;
	    }
	 
	    public static String getSgin(String openid, Integer amount) {
	        sortedMap.put("mch_appid","appid");
	        sortedMap.put("mchid","商户号");
	        sortedMap.put("nonce_str", WeixinpayUtil.getUUID());
	        sortedMap.put("partner_trade_no", WeixinpayUtil.getOrderId());
	        sortedMap.put("openid", openid);
	        sortedMap.put("check_name", "NO_CHECK");
	        sortedMap.put("amount", amount.toString());
	        sortedMap.put("desc", "desc");
	        sortedMap.put("spbill_create_ip", "ip");
	        sortedMap.put("re_user_name", "null");
	        return WeixinpayUtil.getSignCode(sortedMap,"api秘钥");
	    }

}
